//
//  ShareToWeiXinLayer.cpp
//  PlantNanny
//
//  Created by Joueu on 14-12-18.
//
//

#include "ShareToWeiXinLayer.h"
#include "SocialUtils.h"

bool ShareToWeiXinLayer::init()
{
    if (!Layer::init()) {
        return false;
    }
    Size visibleSize = Director::getInstance()->getVisibleSize();
    shaderLayer = ShaderLayer::create();
    this -> addChild(shaderLayer, 5000);
    
    popLayer = PopupLayer::create();
    this -> addChild(popLayer, 5001);
    
    auto description = Sprite::create("share.png");
    description -> setPosition(Vec2(visibleSize.width * 0.5f, visibleSize.height * 0.5f));
    popLayer -> addChild(description, 5);
    
    auto closeBtn = ui::Button::create("btn_close@2x.png");
    closeBtn -> setScale(1.2f);
    closeBtn -> setAnchorPoint(Point(0.3f, 0.7f));
    closeBtn -> addTouchEventListener(CC_CALLBACK_2(ShareToWeiXinLayer::closeBtnPressed, this));
    closeBtn -> setPosition(Point(0,  description -> getContentSize().height));
    description -> addChild(closeBtn);

    auto shareToFriedBtn = ui::Button::create("shareToFriend.png","shareToFriend_p.png");
    shareToFriedBtn -> addTouchEventListener(CC_CALLBACK_2(ShareToWeiXinLayer::shareToFriendBtnPressed, this));
    shareToFriedBtn -> setPosition(Vec2(description -> getContentSize().width * 0.5f, description -> getContentSize().height * 0.2f));
    description -> addChild(shareToFriedBtn);
    
    auto shareToCricleBtn = ui::Button::create("shareToCircle.png","shareToCircle_p.png");
    shareToCricleBtn -> addTouchEventListener(CC_CALLBACK_2(ShareToWeiXinLayer::shareToCircleBtnPressed, this));
    shareToCricleBtn -> setPosition(Vec2(description -> getContentSize().width * 0.5f, description -> getContentSize().height * 0.40f));
    description -> addChild(shareToCricleBtn);
    //屏蔽在popUp上的触摸
    auto listener = EventListenerTouchOneByOne::create();
    listener -> setSwallowTouches(true);
    listener -> onTouchBegan = [](Touch *touch, Event *event){
        auto target = static_cast<Sprite *>(event -> getCurrentTarget());
        Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
        Size s = target->getContentSize();
        Rect rect = Rect(0, 0, s.width, s.height);
        if(rect.containsPoint(locationInNode)){
            //这里return true swallowTouches才有效
            return true;
        }
        return false;
    };
    
    Director::getInstance() -> getEventDispatcher() -> addEventListenerWithSceneGraphPriority(listener, description);
    

    
    shaderLayer -> setClickBgBlock([&](){
        CocosDenshion::SimpleAudioEngine::getInstance() -> playEffect("negative.mp3");
        ActionInterval *action = ScaleTo::create(0.3f, 0.1f);
        CallFunc *func = CallFunc::create([&](){
            popLayer -> removeFromParentAndCleanup(true);
            shaderLayer -> removeFromParentAndCleanup(true);
        });
        
        popLayer -> runAction(Sequence::create(action, func, NULL));
        
    });
    
    return true;
}

void ShareToWeiXinLayer::shareToFriendBtnPressed(Ref *pSender, ui::Widget::TouchEventType type)
{
    if(type == ui::Widget::TouchEventType::ENDED){
        SocialUtils::registerWeixin("wxf3c2d0ff6b74610b");
        SocialUtils::setShareType(1);
        SocialUtils::shareToWeiXin();
    }
}

void ShareToWeiXinLayer::shareToCircleBtnPressed(Ref *pSender, ui::Widget::TouchEventType type)
{
    if(type == ui::Widget::TouchEventType::ENDED){
        SocialUtils::registerWeixin("wxf3c2d0ff6b74610b");
        SocialUtils::setShareType(0);
        SocialUtils::shareToWeiXin();
    }
}

void ShareToWeiXinLayer::closeBtnPressed(Ref *pSender, ui::Widget::TouchEventType type)
{
    if(type == ui::Widget::TouchEventType::ENDED)
    {
        CocosDenshion::SimpleAudioEngine::getInstance() -> playEffect("negative.mp3");
        ActionInterval *action = ScaleTo::create(0.3f, 0.1f);
        CallFunc *func = CallFunc::create([&](){
            popLayer -> removeFromParentAndCleanup(true);
            shaderLayer -> removeFromParentAndCleanup(true);
        });
        
        popLayer -> runAction(Sequence::create(action, func, NULL));
    }
}